package com.ty.controller;

import cn.hutool.core.thread.ThreadUtil;
import com.ty.dto.Order;
import com.ty.dto.OrderQuery;
import com.ty.dto.OrderingDto;
import com.ty.dto.OrderingOk;
import com.ty.service.IOderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/api/order")
@Slf4j
public class OrderingController implements IOderService {
    @Override
    public OrderingOk ordering(OrderingDto dto) {
        System.out.println(dto);
        OrderingOk ok = new OrderingOk().setId(1L).setNeedPay(BigDecimal.TEN);
        return ok;
    }

    @Override
    public List<Order> select(OrderQuery query) {
        List<Order>  orders = new ArrayList<>();
        for (int i = 1; i <= 2; i++){
            orders.add(new Order().setId(Long.valueOf( i)).setUserId(query.getUserId())
                    .setTotalPrice(BigDecimal.TEN));
        }

        return orders;
    }

    @Override
    public String sleep(Integer time) {
        ThreadUtil.safeSleep( time*1000);
        return "hello";
    }
   /* @RequestMapping
    public OrderingOk ordering(@RequestBody OrderingDto dto){
        System.out.println(dto);

        OrderingOk ok = new OrderingOk().setId(1L).setNeedPay(BigDecimal.TEN);
        return ok;
    }

    @GetMapping
    public List<Order> ordering(OrderQuery query) {

        List<Order>  orders = new ArrayList<>();
        for (int i = 1; i <= 2; i++){
            orders.add(new Order().setId(Long.valueOf(i)).setUserId(query.getUserId())
                    .setTotalPrice(BigDecimal.TEN));
        }

        return orders;
    }

    @GetMapping("/sleep")
    public String sleep (@RequestParam Integer time,HttpServletRequest request) {
        String token = request.getHeader("token");

      *//*  String traceId = request.getHeader("traceId");
        System.out.println("traceId:"+traceId);

      ServletRequestAttributes requestAttributes =  (ServletRequestAttributes)(RequestContextHolder.getRequestAttributes());
        String traceId2 = requestAttributes.getRequest().getHeader("traceId");
        System.out.println("traceId2:"+traceId2);

        System.out.println(traceId);*//*
        log.debug("请求进来了,token:{}",token);
        ThreadUtil.safeSleep( time*1000);
        return "cold";
    }*/

}
